package graduate.cslg.com.universityshopping.dormitory;

import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import graduate.cslg.com.universityshopping.R;
import graduate.cslg.com.universityshopping.net.LoginResponse;
import graduate.cslg.com.universityshopping.net.MyDorBean;
import graduate.cslg.com.universityshopping.net.RetrofitDevelop;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class NewBedActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText stuAccount;
    private EditText stuPass;
    private Button submitButton;
    private String getAccount, getPassword;
    private LoginResponse mLoginResponse;
    private MyDorBean mMyDorBean;
    private String bedInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_bed);
        initViews();
    }

    private void initViews() {
        stuAccount = findViewById(R.id.stu_account);
        stuPass = findViewById(R.id.stu_password);
        submitButton = findViewById(R.id.submit_button);
        submitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                getAccount = stuAccount.getText().toString();
                getPassword = stuPass.getText().toString();
                if (getAccount.length() != 0 && getPassword.length() != 0) {
                    postLogin(getAccount, getPassword);
                }
                break;
        }
    }

    private void postLogin(String account, String password) {
        RetrofitDevelop.getTestService()
                .test_loginCheck(account, password)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginResponse>() {
                    @Override
                    public void onCompleted() {
                        if (mLoginResponse.getStatus().equals("true")) {
                            Toast.makeText(NewBedActivity.this, "床位信息查询中...", Toast.LENGTH_SHORT).show();
                            getBedInfo(stuAccount.getText().toString());
                        } else {
                            Toast.makeText(NewBedActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(LoginResponse loginResponse) {
                        mLoginResponse = loginResponse;
                    }
                });
    }

    private void getBedInfo(String stuAccount) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        RetrofitDevelop.getTestService()
                .getDocInfo(stuAccount)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyDorBean>() {
                    @Override
                    public void onCompleted() {
                        bedInfo = mMyDorBean.getStu_dorinfo();
                        dialog.setTitle("提示");
                        dialog.setMessage("您的床位信息为：\n" + bedInfo);
                        dialog.setPositiveButton("确定",null);
                        dialog.show();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(MyDorBean myDorBean) {
                        mMyDorBean = myDorBean;
                    }
                });
    }
}
